using System;
using System.Collections;
using System.Data;
using CE.Database;
/// <summary>
/// Summary description for UserProfileDB
/// </summary>
/// 
namespace CE.Provider
{
    public class CarYearDB
    {
        public CarYearDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(CarYear caryear)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarYearInsertUpdateSingleItem]";
            
            SortedList _sPara = new SortedList();
            
            #region Parameter
            _sPara.Add("@ProviderCarYearId", caryear.ProviderCarYearId);
            _sPara.Add("@ProviderCompanyId", caryear.ProviderCompanyId);
            _sPara.Add("@CXCoverTypeId", caryear.CXCoverTypeId);
            _sPara.Add("@FromYear", caryear.FromYear);
            _sPara.Add("@ToYear", caryear.ToYear);
            _sPara.Add("@YearRate", caryear.YearRate);
            _sPara.Add("@RefCEUserId", caryear.RefCEUserId);
            _sPara.Add("@Status", caryear.Status);
            _sPara.Add("@Finfo1", caryear.FInfo1);
            _sPara.Add("@Dinfo1", caryear.DInfo1);

            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public bool Update()
        {
            return true;
        }

        public bool Delete(Int64 ProviderCarYearId, Int64 ProviderCompanyId)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarYearDeleteSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ProviderCarYearId", ProviderCarYearId);
            _sPara.Add("@ProviderCompanyId", ProviderCompanyId);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result > 0;
        }

        public CarYearList GetProviderCarYearList(Int64 ProviderCompanyId)
        {
            CarYearList _caryearlist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarYearSelectItemList";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _caryearlist = new CarYearList();
                    for (int i = 0; i < _dt.Rows.Count; i++)
                    {
                        CarYear _caryear = new CarYear();
                        _caryear = FillDataRecord(_dt, i);
                        _caryearlist.Add(_caryear);
                    }
                }
            }
            return _caryearlist;
        }


        public CarYear GetProviderCarYear(Int64 ProviderCompanyId, Int32 CXCoverTypeId)
        {
            CarYear _caryear = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarYearSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@CXCoverTypeId", CXCoverTypeId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _caryear = new CarYear();
                    _caryear = FillDataRecord(_dt, 0);
                }
            }
            return _caryear;
        }

        public CarYear GetProviderCarYear(int FromYear, int ToYear,Int64 ProviderCompanyId, Int32 CXCoverTypeId)
        {
            CarYear _caryear = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarYearSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@CXCoverTypeId", CXCoverTypeId);
            _spara.Add("@FromYear", FromYear);
            _spara.Add("@ToYear", ToYear);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _caryear = new CarYear();
                    _caryear = FillDataRecord(_dt, 0);
                }
            }
            return _caryear;
        }

        public bool CheckProviderCarYearDuplicate(Int64 CurrentProviderCarYearId, int FromYear, int ToYear, Int64 ProviderCompanyId, Int32 CXCoverTypeId)
        {
            bool _retValue = false;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarYearDuplicateSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCarYearId", CurrentProviderCarYearId);
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@CXCoverTypeId", CXCoverTypeId);
            _spara.Add("@FromYear", FromYear);
            _spara.Add("@ToYear", ToYear);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _retValue = true;
                }
            }
            return _retValue;
        }

        public CarYear GetProviderCarYear(Int64 ProviderCarYearId)
        {
            CarYear _caryear = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarYearSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@CarYearId", ProviderCarYearId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _caryear = new CarYear();
                    _caryear = FillDataRecord(_dt, 0);
                }
            }
            return _caryear;
        }

        private CarYear FillDataRecord(DataTable myDataTable, int RowId)
        {
            CarYear myCarYear = new CarYear();
            myCarYear.ProviderCarYearId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCarYearId"].ToString());
            myCarYear.ProviderCompanyId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCompanyId"].ToString());
            myCarYear.FromYear = Convert.ToInt32(myDataTable.Rows[RowId]["FromYear"].ToString());
            myCarYear.ToYear = Convert.ToInt32(myDataTable.Rows[RowId]["ToYear"].ToString());
            myCarYear.CXCoverTypeId = Convert.ToInt32(myDataTable.Rows[RowId]["CXCoverTypeId"].ToString());
            myCarYear.YearRate = Convert.ToSingle(myDataTable.Rows[RowId]["YearRate"].ToString());
            myCarYear.RefCEUserId = Convert.ToInt64(myDataTable.Rows[RowId]["RefCEUserId"].ToString());
            myCarYear.Status = Convert.ToBoolean(myDataTable.Rows[RowId]["Status"].ToString());

            if (myDataTable.Rows[RowId]["FInfo1"].ToString() != "")
                myCarYear.FInfo1 = Convert.ToSingle(myDataTable.Rows[RowId]["FInfo1"].ToString());

            if (myDataTable.Rows[RowId]["DInfo1"].ToString() != "")
                myCarYear.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["DInfo1"].ToString());
            else
                myCarYear.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());
            return myCarYear;
        }
        
    }
}